import type { Module, ActionContext } from 'vuex'
import type { AxiosResponse } from 'axios'
import type { OrderStateType } from './typings'
import {
  getOrderSetList,
  postOrderSetAdd,
  getOrderSetView,
  postOrderSetUpdate,
  getOrderCountInfo,
  getOrderList,
  getOrderInfo,
  postOrderAdd,
  postSaveAndSendOrder,
  postOrderUpdate,
  getExistOrderMapMarkerList,
  getTenantUserSelectList,
  getDeptTreeSelectList,
  postOrderSetRemove,
  getConfirmOrderHandleUserSelectList,
  getOrderManageProcessRecordListByBusinessKey,
  getOperationRecordLisByOrderId,
  getDeptConfigList,
  getDeptWorkareaList,
  postOrderSend,
  postOrderRemove,
  postOrderConfirm,
  postOrderBack,
  postOrderEnd,
  postOrderRegular,
  postDeptConfigAdd,
  getDeptConfigView,
  postDeptConfigUpdate,
  postDeptConfigRemove
} from '~/api/oam/order'
import { getDataDictionaryByCode } from '~/api/global'
import { filterValueEnum, handleTreeTransformArray } from '~/utils/util'

const orderQualityServiceSelectList = [
  {
    key: 0,
    value: '满意'
  },
  {
    key: 1,
    value: '不满意'
  },
  {
    key: 2,
    value: '需跟进'
  }
]

const order: Module<OrderStateType, OrderStateType> = {
  namespaced: true,
  state: {
    // 工单状态
    orderStatusSelectList: [],
    tenantUserSelectList: [],
    deptTreeSelectList: [],
    handleUserSelectList: [],
    orderQualityServiceSelectList,
    orderStatusEnum: {},
    tenantUserEnum: {},
    deptTreeEnum: {},
    appointmentTimeEnum: {
      0: '否',
      1: '是'
    }
  },
  getters: {
    orderStatusSelectList: (state) => state.orderStatusSelectList,
    tenantUserSelectList: (state) => state.tenantUserSelectList,
    deptTreeSelectList: (state) => state.deptTreeSelectList,
    handleUserSelectList: (state) => state.handleUserSelectList,
    orderQualityServiceSelectList: (state) => state.orderQualityServiceSelectList,
    orderStatusEnum: (state) => state.orderStatusEnum,
    tenantUserEnum: (state) => state.tenantUserEnum,
    deptTreeEnum: (state) => state.deptTreeEnum,
    appointmentTimeEnum: (state) => state.appointmentTimeEnum
  },
  mutations: {
    SET_ORDER_TYPE_SELECT_LIST(state, payload) {
      state.orderStatusSelectList = payload
      state.orderStatusEnum = filterValueEnum(
        payload,
        'dictKey',
        'dictValue'
      ) as OrderStateType['orderStatusEnum']
    },
    SET_TENANTUSERSELECTLIST: (state, payload) => {
      state.tenantUserSelectList = payload
      state.tenantUserEnum = filterValueEnum(
        payload,
        'id',
        'name'
      ) as OrderStateType['tenantUserEnum']
    },
    SET_DEPTTREESELECTLIST: (state, payload) => {
      state.deptTreeSelectList = payload
      state.deptTreeEnum = filterValueEnum(
        handleTreeTransformArray(payload, 'children'),
        'value',
        'title'
      ) as OrderStateType['deptTreeEnum']
    },
    SET_HANDLEUSERSELECTLIST: (state, payload) => {
      state.handleUserSelectList = payload
    }
  },
  actions: {
    // 获取 工单设置 列表
    GetOrderSetList<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getOrderSetList<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 获取 工单设置 字典
    GetDataDictionaryByCode<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getDataDictionaryByCode<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 工单设置 新增
    PostOrderSetAdd<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        postOrderSetAdd<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 工单设置 编辑 查看
    GetOrderSetView<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getOrderSetView<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 工单设置 编辑 更新
    PostOrderSetUpdate<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        postOrderSetUpdate<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 工单设置 编辑 删除
    PostOrderSetRemove<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        postOrderSetRemove<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 工单设置 运维部门 主列表
    GetDeptConfigList<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getDeptConfigList<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 工单设置 运维部门 新增 选择对应工区 列表
    GetDeptWorkareaList<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getDeptWorkareaList<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 工单设置 运维部门 新增->确定
    PostDeptConfigAdd<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        postDeptConfigAdd<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 工单设置 运维部门 编辑->查看
    GetDeptConfigView<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getDeptConfigView<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 工单设置 运维部门 编辑->确定
    PostDeptConfigUpdate<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        postDeptConfigUpdate<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 工单设置 运维部门 删除
    PostDeptConfigRemove<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        postDeptConfigRemove<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 获取工单历史统计数据
    GetOrderCountInfo() {
      return new Promise((resolve, reject) => {
        getOrderCountInfo()
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 查询工单列表
    GetOrderList<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getOrderList<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 查询工单详情
    GetOrderInfo<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getOrderInfo<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 新增工单 保存
    PostOrderAdd<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        postOrderAdd<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 新增工单 保存并发送
    PostSaveAndSendOrder<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        postSaveAndSendOrder<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 编辑工单
    PostOrderUpdate<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        postOrderUpdate<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 获取工单现存工单数据（地图标记点）
    GetExistOrderMapMarkerList<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getExistOrderMapMarkerList<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 工单类型下拉列表
    async GetOrderTypeSelectList<S, T>({ commit }: ActionContext<S, T>) {
      return await getDataDictionaryByCode({ code: 'orm_order_status' }).then(
        (res: AxiosResponse) => {
          commit('SET_ORDER_TYPE_SELECT_LIST', res?.data?.data || [])
          return res?.data
        }
      )
    },

    // 获取租户用户下拉列表
    GetTenantUserSelectList<S, T>({ commit }: ActionContext<S, T>) {
      return new Promise((resolve, reject) => {
        getTenantUserSelectList()
          .then((res: AxiosResponse) => {
            commit('SET_TENANTUSERSELECTLIST', res?.data?.data || [])
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 获取设备所属部门下拉列表
    GetDeptTreeSelectList<S, T>({ commit }: ActionContext<S, T>) {
      return new Promise((resolve, reject) => {
        getDeptTreeSelectList()
          .then((res: AxiosResponse) => {
            commit('SET_DEPTTREESELECTLIST', res?.data?.data || [])
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 确认（处理）工单时工单负责人下拉列表
    GetConfirmOrderHandleUserSelectList<S, T>({ commit }: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getConfirmOrderHandleUserSelectList<T>(params)
          .then((res: AxiosResponse) => {
            commit('SET_HANDLEUSERSELECTLIST', res?.data?.data || [])
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 获取流程记录（工单管理） BusinessKey = orderId
    GetOrderManageProcessRecordListByBusinessKey<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getOrderManageProcessRecordListByBusinessKey<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 获取操作记录列表
    GetOperationRecordLisByOrderId<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getOperationRecordLisByOrderId<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 工单发送
    PostOrderSend<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        postOrderSend<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 工单删除
    PostOrderRemove<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        postOrderRemove<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 确认工单
    PostOrderConfirm<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        postOrderConfirm<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 回退工单
    PostOrderBack<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        postOrderBack<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 终结工单
    PostOrderEnd<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        postOrderEnd<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 归档工单
    PostOrderRegular<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        postOrderRegular<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    }
  }
}

export default order
